package com.enfry.common.util.excel;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class HSSFExcelParser extends ExcelParser {
	
	@Override
	public List[] parseExcel(Map context, InputStream is) throws Exception {
		HSSFWorkbook workbook = null;
		try{
			try {
				workbook = new HSSFWorkbook(is);
			} catch (Exception e) {
				throw new Exception("读取Excel出错:" + e.getMessage(), e);
			}
	
			// 判断实际读入的sheet的数目是否少于期望的sheet的数目
			int sheetSize = workbook.getNumberOfSheets();
			int parserSize = sheetParsers.size();
			if (sheetSize < parserSize) {
				throw new Exception("Excel文件的表单数(" + sheetSize + ")少于期望的数目("
						+ parserSize + ")");
			}
	
			List[] result = new List[parserSize];
			for (int i = 0; i < parserSize; i++) {
				// 模板中有多个sheet，有些sheet不需要进行解析，所以要根据sheet名称获取需要sheet进行解析
				SheetParser parser = sheetParsers.get(i);
				result[i] = parser.parseSheet(context, workbook.getSheetAt(i));
			}
			return result;
		}finally{
			if(workbook!=null){
				workbook.close();
			}
		}
	}
}
